package com.groupbuying.provider.service.impl;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.groupbuying.provider.connector.FileFegin;
import com.groupbuying.provider.constants.StoreCustomerCollectEnumConstants;
import com.groupbuying.provider.dto.StorePhotoDTO;
import com.groupbuying.provider.dto.app.StoreCollectDTO;
import com.groupbuying.provider.dto.app.StoreCollectPageDTO;
import com.groupbuying.provider.dto.app.StoreCollectQueryDTO;
import com.groupbuying.provider.dto.convert.StoreCollectDTOConvert;
import com.groupbuying.provider.utils.Query;
import com.groupbuying.provider.utils.apiresult.AppApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.groupbuying.provider.dao.StoreCustomerCollectDao;
import com.groupbuying.provider.domain.StoreCustomerCollectDO;
import com.groupbuying.provider.service.StoreCustomerCollectService;


@Service
public class StoreCustomerCollectServiceImpl implements StoreCustomerCollectService {
    @Autowired
    private StoreCustomerCollectDao storeCustomerCollectDao;

    @Autowired
    private FileFegin fileFegin;

    @Override
    public StoreCustomerCollectDO get(String id) {
        return storeCustomerCollectDao.get(id);
    }

    @Override
    public List<StoreCustomerCollectDO> list(Map<String, Object> map) {
        return storeCustomerCollectDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return storeCustomerCollectDao.count(map);
    }

    @Override
    public int save(StoreCustomerCollectDO storeCustomerCollect) {
        return storeCustomerCollectDao.save(storeCustomerCollect);
    }

    @Override
    public int update(StoreCustomerCollectDO storeCustomerCollect) {
        return storeCustomerCollectDao.update(storeCustomerCollect);
    }

    @Override
    public int remove(String id) {
        return storeCustomerCollectDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        return storeCustomerCollectDao.batchRemove(ids);
    }


    @Override
    public Object queryCollectList(Map<String, Object> params) {

        /**
         * 会员uuid
         */
        Object temp = params.get("customerUuid");
        String customerUuid = temp == null ? "" : temp.toString();
        /**
         * 页码
         */
        temp = params.get("page");
        String page = temp == null ? "" : temp.toString();
        /**
         * 每页显示多少
         */
        temp = params.get("pageShow");
        Integer pageShow = temp == null ? null : Integer.parseInt(temp.toString());


        if (StringUtils.isBlank(customerUuid) || StringUtils.isBlank(page)  || pageShow == null) {
            return AppApiResult.failure("param is null");
        }
        params.put("isDelete", StoreCustomerCollectEnumConstants.ENUM_ISDELETE_UNDEL);
        params.put("limit",pageShow);
        params.put("sort", "gmt_create");
        params.put("order", "desc");

        Query query = new Query(params);
        List<StoreCollectQueryDTO> list = storeCustomerCollectDao.queryCollectShopList(query);

        if (list == null || !(list.size() > 0)) {
            return AppApiResult.success(new StoreCollectPageDTO(0, 0, pageShow, new ArrayList<StoreCollectDTO>()));
        }

        int dataSize = list.size();
        List<StoreCollectDTO> storeCollectDTOList = new ArrayList<>(list.size());
        StoreCollectDTO storeCollectDTO = null;
        StorePhotoDTO storePhotoDTO = null;
        for (StoreCollectQueryDTO storeCollectQueryDTO : list) {
            storeCollectDTO = StoreCollectDTOConvert.MAPPER.convertDTO(storeCollectQueryDTO);
            storePhotoDTO = new StorePhotoDTO();
            storePhotoDTO.setSlideshow(fileFegin.getDownloadUrlByFileGroupKey(storeCollectQueryDTO.getSlideshow()));
            storePhotoDTO.setDoorPhoto(fileFegin.getDownloadUrlByFileGroupKey(storeCollectQueryDTO.getDoorPhoto()));
            storePhotoDTO.setWithinPhoto(fileFegin.getDownloadUrlByFileGroupKey(storeCollectQueryDTO.getWithinPhoto()));
            storeCollectDTO.setStorePhotoDTO(storePhotoDTO);
            storeCollectDTO.setFeedback(new BigDecimal(100));
            storeCollectDTOList.add(storeCollectDTO);
        }
        int pageTotal = Double.valueOf(Math.ceil((double) dataSize / (double) pageShow)).intValue();


        return AppApiResult.success(new StoreCollectPageDTO(list.size(), 0, pageShow, storeCollectDTOList));
    }
}
